package itemCF.step3;


import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @author legolas
 * @date 2020/3/13 下午7:06
 */

public class Mapper3 extends Mapper<LongWritable, Text, Text, Text> {

    /**
     * 需要实现的map函数
     * 这个map函数就是可以接收k1，v1   产生k2，v2
     *
     * @param k1
     * @param v1
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */

    @Override
    protected void map(LongWritable k1, Text v1, Context context) throws IOException, InterruptedException {
        Text k2 = new Text();
        Text v2 = new Text();

        String[] rowAndLine = v1.toString().split("\t");

        //行号(物品id)
        String rownum = rowAndLine[0];
        String[] lines = rowAndLine[1].split(",");
        //["A_2.0","B_0","C_5.0"]
        for (int i = 0; i < lines.length; i++) {
            String column = lines[i].split("_")[0];
            String valueStr = lines[i].split("_")[1];
            //key 列号 value 行号_值
            k2.set(column);
            v2.set(rownum + "_" + valueStr);
            context.write(k2, v2);
        }
    }
}
